//
//  BaseHiddenTabBarController.m
//  coldjoke-ios-client
//
//  Created by ColdWorks on 12-8-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BaseHiddenTabBarController.h"

@implementation BaseHiddenTabBarController

@synthesize tabItems = _tabItems;
@synthesize currentIndex;
@synthesize lastIndex;

- (id) init
{
    self = [super init];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self prepare];
    [self hideRealTabBar];
    [self initCustomTabBar];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) dealloc{
    PP_RELEASE(_tabItems);
	[super dealloc];
}

- (void) prepare
{
    currentIndex = 0;
    lastIndex = -1;
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"hideTabBar" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(hideTabBar)
                                                 name: @"hideTabBar"
                                               object: nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"showTabBar" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(showTabBar)
                                                 name: @"showTabBar"
                                               object: nil];
}

- (void)hideRealTabBar{
	for(UIView *view in self.view.subviews){
		if([view isKindOfClass:[UITabBar class]]){
			view.hidden = YES;
			break;
		}
	}
    UIView *contentView;
    if ( [[self.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]] ){
        contentView = [self.view.subviews objectAtIndex:1];
    }else{
        contentView = [self.view.subviews objectAtIndex:0];
    }
    contentView.frame = CGRectMake(contentView.bounds.origin.x,  contentView.bounds.origin.y,  contentView.bounds.size.width, contentView.bounds.size.height + self.tabBar.frame.size.height);
}

//自定义tabbar
- (void)initCustomTabBar{
	//创建按钮
	int viewCount = self.viewControllers.count < 5 ? 5 : self.viewControllers.count;
	self.tabItems = [NSMutableArray arrayWithCapacity:viewCount];
	double _width = 320 / viewCount;
	double _height = self.tabBar.frame.size.height;
	for (int i = 0; i < viewCount; i++) {
		UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
		btn.frame = CGRectMake(i*_width, 0, _width, _height);
		[btn addTarget:self action:@selector(selectedTab:) forControlEvents:UIControlEventTouchDown];
		btn.tag = i;
		[self.tabItems addObject:btn];
	}
}

//切换tabbar
- (void)selectedTab:(UIButton *)button
{
	if (currentIndex == button.tag) {
		[[self.viewControllers objectAtIndex:button.tag] popToRootViewControllerAnimated:YES];
		return;
	}
    lastIndex = currentIndex;
	currentIndex = button.tag;
	self.selectedIndex = currentIndex;
}

//切换tabbar
- (void)select:(int)index{
    Log(@"index :%d",index);
    UIButton *button = (UIButton *)[self.tabItems objectAtIndex:index];
    [self selectedTab:button];
}

//自定义tabbar
- (void)showTabBar{
    Log(@"mark");
}

//隐藏tabbar
- (void)hideTabBar{
    Log(@"mark");
//    [self performSelector:@selector(hideRealTabBar)];
}

@end
